#include "classifierprtdelegate.h"
#include <QLineEdit>
#include <QGridLayout>
#include "EditorWidgets/classifierprteditor.h"
#include "dataview/classifierproductmodel.h"
#include <QPainter>
#include <QDebug>

ClassifierPrtDelegate::ClassifierPrtDelegate(QObject *parent) : QAbstractItemDelegate(parent)
{

}

QSize ClassifierPrtDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    return QSize(0,140);
}

void ClassifierPrtDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    painter->save();
    QColor color(30, 60, 150);
    QString showName = index.data(ClassifierProductModel::ROLE_NAME).toString();
    QString item = "";
    int t = 0;
    QStringList strList;

    for(int i =ClassifierProductModel::ROLE_R1_LOW;i<=ClassifierProductModel::ROLE_R12_HI;i++)
    {

        if(t%2==0)
        {
            item += QString("R%1:").arg((i-ClassifierProductModel::ROLE_R1_LOW)/2+1);
            item += "[";
            item += QString::number(index.data(i).toFloat());
        }
        else
        {
            item += "-";
            item += QString::number(index.data(i).toFloat());
            item += "] ";
            strList<<item;
            item.clear();
        }
        t++;
    }

    QList<QRect> rlist;
    QRect rname;
    QRect r = option.rect;
    int w = r.width();
    int h = r.height();
    int rw = (w-50)/4;
    int rh = h/3;
    int x1 = r.left();int y1 = r.top();
    rname = QRect(r.topLeft(),QSize(50,h));
    for(int i = 1;i<=12;i++)
    {
        rlist<<QRect(QPoint(x1+50+rw*((i-1)%4),y1+rh*((i-1)/4)),QSize(rw,rh));
    }
    if(!showName.isEmpty())
    {
        if(option.state&QStyle::State_Selected)
        {
            painter->setBrush(Qt::darkBlue);
        }
        else {

            painter->setBrush(color);
        }
    }
    painter->drawRect(option.rect);
    painter->setPen(Qt::white);
    painter->setFont(QFont("Ubuntu Bold",15));
    painter->drawText(rname,showName,Qt::AlignVCenter|Qt::AlignHCenter);
    for(int i = 0;i<12;i++)
    {
        painter->drawText(rlist.at(i),strList.at(i),Qt::AlignVCenter|Qt::AlignHCenter);
    }
//    painter->drawText(rline1,line1,Qt::AlignVCenter|Qt::AlignHCenter);
//    painter->drawText(rline2,line2,Qt::AlignVCenter|Qt::AlignHCenter);
//    painter->drawText(rline3,line3,Qt::AlignVCenter|Qt::AlignHCenter);
    painter->restore();
}

